package view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.List;

import javax.swing.ButtonGroup;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import modell.Knapp;
import modell.Viser;
import modell.ViserKontroller;

public class ValgPanel extends JPanel implements ActionListener{
	private static final long serialVersionUID = 4794339266869856047L;
	
	private ButtonGroup group;
	private JRadioButton time, minutt, sekund;
	private List<ViserKontroller> visere;
	private Knapp oppKnapp, nedKnapp;
	private Viser valgtViser;
	
	public ValgPanel(Knapp opp, Knapp ned, ViserKontroller ... visere){
		
		this.oppKnapp = opp;
		this.nedKnapp = ned;
		this.visere = Arrays.asList(visere);
		
		opprettRadioButtons();
	}

	private void opprettRadioButtons() {
		group = new ButtonGroup();
		time = new JRadioButton();
		minutt = new JRadioButton();
		sekund = new JRadioButton();
		
		sekund.setSelected(true);
		sekund.addActionListener(this);
		minutt.addActionListener(this);
		time.addActionListener(this);
		
		time.setActionCommand("0");
		minutt.setActionCommand("1");
		sekund.setActionCommand("2");
		
		group.add(time);
		group.add(minutt);
		group.add(sekund);
		
		add(time);
		add(minutt);
		add(sekund);
	}

	private void velgViser(ActionEvent e){
		valgtViser = (Viser) visere.get(Integer.valueOf(e.getActionCommand()));
	}
	
	private void setViser(){
		oppKnapp.setViser(valgtViser);
		nedKnapp.setViser(valgtViser);
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		velgViser(e);
		setViser();
	}

}
